package com.neuedu.Servlet;

import javax.servlet.*;
import javax.servlet.http.*;
import javax.servlet.annotation.*;
import java.io.IOException;
import java.io.PrintWriter;

@WebServlet("/LoginServlet")
public class LoginServlet extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        request.setCharacterEncoding("UTF-8");
        response.setContentType("text/html; charset=UTF-8");
        response.getWriter().write("nihaoa我的世界");


    }

    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
//    可以直接选择 new->servlet  就会创建一个新的类
//    或者在idea上使用添加快捷键的方式直接添加Servlet的类(alt+s)
  //        指定post的解码方式
            request.setCharacterEncoding("UTF-8");
            String name=request.getParameter("name");
            String password=request.getParameter("password");
            /*System.out.println("用户名："+name);
            System.out.println("密码："+password);*/

//        判断账户密码是否相同
//        避免空指针异常
        String html=null;
        if ("张三".equals(name)&&"123456".equals(password)){
            html="<div align='center'>cod:1,msg:'success'</div>";
        }else {
            html="<div align='center'>cod:2,msg:'failed'</div>";
        }
        PrintWriter pw=response.getWriter();
        pw.println(html);
        }
    }
